package com.youjia.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.youjia.mapper.ItemMapper;
import com.youjia.pojo.Item;
import com.youjia.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private ItemMapper itemMapper;
//    @Autowired
//    private ItemDescMapper itemDescMapper;

    @Override
    public PageResult getItemList(PageResult pageResult) {
        IPage<Item> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "title", pageResult.getQuery());
        page = itemMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Item> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void updateItemStatus(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void deleteItemById(Integer id) {
        itemMapper.deleteById(id);
        //itemDescMapper.deleteById(id);
    }

//    @Override
//    @Transactional
//    public void saveItem(ItemVO itemVO) {
//        Item item = itemVO.getItem();
//        item.setStatus(true);
//        itemMapper.insert(item);
//        ItemDesc itemDesc = itemVO.getItemDesc();
//        itemDesc.setId(item.getId());
//        itemDescMapper.insert(itemDesc);
//    }

    @Override
    @Transactional
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }
}
